﻿window.OrderDetailView = Backbone.View.extend({
    el: '#jqt',
    initialize: function () {
        this.model = new Order();
        this.template = $('#OrderDetailTemplate').html();
    },
    events: {
        'click #OrderDetail ul.orderPayment': 'goToPayDetail',
        'click #OrderDetail ul.fetchDetail li': 'goToProductDetail'
    },
    render: function () {
        var partial = { header: $('#HeaderTemplate').html() };
        var data = { hasBack: true, title: '返回订单列表', btnListR: [{ name: 'avatar' }, { name: 'home'}] };

        this.$el.append(Mustache.render(this.template, data, partial));

        this.fetchDetail();

        return this;
    },

    fetchDetail: function () {
        var that = this;
        this.model.fetch({
            success: function () {
                var template = $('#OrderDetailHeadTemplate').html();
                $('#OrderDetail .orderDetail').append(Mustache.render(template, that.model.toJSON(), []));
                $('#OrderDetail .orderDetail').append(Mustache.render($('#OrderDetailPriceTemplate').html(), that.model.toJSON(), []));

                $('#OrderDetail .orderDetail').append(Mustache.render($('#OrderDetailTrackTemplate').html(),[],[]));

                var data = { products: that.model.get('ProductList') };
                $('#OrderDetail .orderDetail').append(Mustache.render($('#PayDetailProductListTemplate').html(), data, []));

                $('#OrderDetail').append(Mustache.render($('#PayDetailPaymentTemplate').html(), that.model.toJSON(), []));
                $('#OrderDetail #hiddenPayDetail').append(Mustache.render($('#PayDetailProductListTemplate').html(), data, []));
                pageView.resizeScroll();
            }
        });
    },

    goToPayDetail: function (e) {
        pageView.goTo('PayDetail');
    },

    goToProductDetail: function (e) {
        var target = $(e.currentTarget);

        localStorage.setItem('productDetail', JSON.stringify({ productid: target.attr('productid'), price: target.attr('productprice') }));
        localStorage.setItem('productid', target.attr('productid'));
        localStorage.setItem('detailType', 'tab_intro');
        localStorage.setItem('commentPageIndex', 1);
        localStorage.setItem('ordercommentPageIndex', 1);

        pageView.goTo('ProductDetail');
    }
});


window.PayDetailView = Backbone.View.extend({
    el: '#jqt',
    initialize: function () {
        this.model = new Order();
        this.template = $('#PayDetailTemplate').html();
    },
    events: {
        'click #PayDetail ul.fetchDetail li': 'goToProductDetail'
    },
    render: function () {
        var partial = { header: $('#HeaderTemplate').html() };
        var data = { hasBack: true, title: '返回', btnListR: [{ name: 'avatar' }, { name: 'home'}] };

        this.$el.append(Mustache.render(this.template, data, partial));

        if ($('#hiddenPayDetail').length > 0) {
            $('.payDetail').html($('#hiddenPayDetail').html());
            pageView.resizeScroll();
        } else {
            var that = this;
            this.model.fetch({
                success: function () {
                    var template = $('#PayDetailPaymentTemplate').html();
                    $('.payDetail').html(Mustache.render(template, that.model.toJSON(), []));

                    var data = { products: that.model.get('ProductList') };
                    $('.payDetail #hiddenPayDetail').append(Mustache.render($('#PayDetailProductListTemplate').html(), data, []));
                    $('.payDetail #hiddenPayDetail').show();
                    pageView.resizeScroll();
                }
            });
        }

        return this;
    },

    goToProductDetail: function (e) {
        var target = $(e.currentTarget);

        localStorage.setItem('productDetail', JSON.stringify({ productid: target.attr('productid'), price: target.attr('productprice') }));
        localStorage.setItem('productid', target.attr('productid'));
        localStorage.setItem('detailType', 'tab_intro');
        localStorage.setItem('commentPageIndex', 1);
        localStorage.setItem('ordercommentPageIndex', 1);

        pageView.goTo('ProductDetail');
    }
});